//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Logging Application Block
//===============================================================================
//  LogParser - Log File Parser
//  Copyright (C) 2007  Alois Kraus
//  
//  This source code is published under the Microsoft Permissive License (Ms-PL)
//  http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx
//===============================================================================

using System;
using System.Globalization;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace EntLibContrib.Logging.Parser
{
	/// <summary>
	/// Encapsulates the logic for parsing the log entry text from a log file.
	/// </summary>
	public class LogEntryParser
	{
		#region Fields
		private ITimeStampParser timeStampParser;
		#endregion

		#region Construction
		/// <summary>
		/// Constructor to initialize a new instance of the class.
		/// </summary>
		public LogEntryParser()
		{
			timeStampParser = new TimeStampParser(CultureInfo.CurrentCulture);
		}
		/// <summary>
		/// Constructor to initialize a new instance of the class.
		/// </summary>
		/// <param name="theTimeStampParser">the time stamp parser to use</param>
		public LogEntryParser(ITimeStampParser theTimeStampParser)
		{
			timeStampParser = theTimeStampParser;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Deserialize the regular expression match values into a log entry object.
		/// </summary>
		/// <param name="match">The match.</param>
		/// <returns>the deserialized log entry</returns>
		public LogEntry DeserializeLogEntry(Match match)
		{
			LogEntry ent = new LogEntry();
			string activityid = match.Groups[RegularExpressionBuilder.ActivityIdToken].Value;
			if (!String.IsNullOrEmpty(activityid))
				ent.ActivityId = new Guid(activityid);
			else
				ent.ActivityId = Guid.Empty;

			ent.AppDomainName = match.Groups[RegularExpressionBuilder.AppDomainNameToken].Value;

			string stringEventId = match.Groups[RegularExpressionBuilder.EventIdToken].Value;
			if (!String.IsNullOrEmpty(stringEventId))
				ent.EventId = int.Parse(stringEventId, CultureInfo.CurrentCulture);

			string stringProperties = match.Groups[RegularExpressionBuilder.DictionaryParameterToken].Value;
			if (!String.IsNullOrEmpty(stringProperties))
				ent.ExtendedProperties["Properties"] = stringProperties;

			ent.MachineName = match.Groups[RegularExpressionBuilder.MachineToken].Value;
			ent.ManagedThreadName = match.Groups[RegularExpressionBuilder.ThreadNameToken].Value;
			ent.Message = match.Groups[RegularExpressionBuilder.MessageToken].Value;
			ent.Categories = new string[] { match.Groups[RegularExpressionBuilder.CategoryToken].Value };

			string stringPriority = match.Groups[RegularExpressionBuilder.PriorityToken].Value;
			if (!String.IsNullOrEmpty(stringPriority))
				ent.Priority = int.Parse(stringPriority, CultureInfo.CurrentCulture);

			ent.ProcessId = match.Groups[RegularExpressionBuilder.ProcessIdToken].Value;
			ent.ProcessName = match.Groups[RegularExpressionBuilder.ProcessNameToken].Value;

			string stringSeverity = match.Groups[RegularExpressionBuilder.SeverityToken].Value;
			if (!String.IsNullOrEmpty(stringSeverity))
				ent.Severity = (TraceEventType)Enum.Parse(typeof(TraceEventType), stringSeverity);
			else
				ent.Severity = TraceEventType.Information;

			string stringTime = match.Groups[RegularExpressionBuilder.TimeStampToken].Value;

			ent.TimeStamp = DateTime.MinValue;
			if (!String.IsNullOrEmpty(stringTime))
				ent.TimeStamp = timeStampParser.ParseTimeStamp(stringTime);

			ent.Title = match.Groups[RegularExpressionBuilder.TitleToken].Value;
			ent.Win32ThreadId = match.Groups[RegularExpressionBuilder.Win32ThreadIdToken].Value;

			return ent;
		}
		#endregion
	}
}
